package com.ruoyi.sycs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.base.domain.SyType;
import com.ruoyi.base.mapper.SyTypeMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.system.service.ISysDictDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.sycs.domain.bo.BiQualificationBo;
import com.ruoyi.sycs.domain.vo.BiQualificationVo;
import com.ruoyi.sycs.domain.BiQualification;
import com.ruoyi.sycs.mapper.BiQualificationMapper;
import com.ruoyi.sycs.service.IBiQualificationService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 技术指标图鉴Service业务层处理
 *
 * @author zyb
 * @date 2024-04-08
 */
@RequiredArgsConstructor
@Service
public class BiQualificationServiceImpl implements IBiQualificationService {

    private final BiQualificationMapper baseMapper;

    private final SyTypeMapper syTypeMapper;
    private final ISysDictDataService dictService;

    /**
     * 查询技术指标图鉴
     */
    @Override
    public BiQualificationVo queryById(Long qualId) {
        return baseMapper.selectVoById(qualId);
    }

    /**
     * 查询技术指标图鉴列表
     */
    @Override
    public TableDataInfo<BiQualificationVo> queryPageList(BiQualificationBo bo, PageQuery pageQuery) {
        if (ObjectUtils.isEmpty(bo.getSyType())) {
            throw new ServiceException("试验类型不能为空");
        }
        LambdaQueryWrapper<BiQualification> lqw = buildQueryWrapper(bo);
        Page<BiQualificationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询技术指标图鉴列表
     */
    @Override
    public List<BiQualificationVo> queryList(BiQualificationBo bo) {
        LambdaQueryWrapper<BiQualification> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BiQualification> buildQueryWrapper(BiQualificationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BiQualification> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getQualName()), BiQualification::getQualName, bo.getQualName());
        lqw.eq(StringUtils.isNotBlank(bo.getQualUnit()), BiQualification::getQualUnit, bo.getQualUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getSyType()), BiQualification::getSyType, bo.getSyType());
        return lqw;
    }

    /**
     * 新增技术指标图鉴
     */
    @Override
    public Boolean insertByBo(BiQualificationBo bo) {
        BiQualification add = BeanUtil.toBean(bo, BiQualification.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setQualId(add.getQualId());
        }
        return flag;
    }

    /**
     * 修改技术指标图鉴
     */
    @Override
    public Boolean updateByBo(BiQualificationBo bo) {
        BiQualification update = BeanUtil.toBean(bo, BiQualification.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BiQualification entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除技术指标图鉴
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 查询技术指标图鉴列表
     *
     * @param bo
     * @return
     */
    @Override
    public List<BiQualification> biList(BiQualificationBo bo) {
        if (ObjectUtils.isEmpty(bo.getSyType())) {
            throw new ServiceException("请选择试验类型");
        }
        // 根据试验ID获取parentId
        SyType syType = syTypeMapper.selectById(bo.getSyType());
        if (ObjectUtils.isNotEmpty(syType.getParentId())) {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("sy_type", syType.getParentId());
            return baseMapper.selectList(wrapper);
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public List<TgsybgBaDetailVo> compute(List<TgsybgBaDetailVo> voList) {
        voList.forEach(v -> {
            if (ObjUtil.isNull(v.getQualId())) {
                throw new ServiceException("qualId不能为空");
            }
            // 1. 根据不同符号 格式化技术指标 && 结果判定
            if (StrUtil.isEmpty(v.getSymbolValue())) {
                v.setJiszb("-");
                if (ObjUtil.isNotNull(v.getJiancjg())) {
                    v.setJiegpd("实测值");
                } else {
                    v.setJiegpd("-");
                }
                return;
            }
            switch (v.getSymbolValue()) {
                case "01":
                    if (ObjUtil.isNotNull(v.getNum1())) {
                        v.setJiszb(dictService.selectDictLabel("symbol",
                            "01") + BigDecimalUtil.toPlain(v.getNum1()));
                        if (ObjUtil.isNotNull(v.getJiancjg())) {
                            if (v.getJiancjg().compareTo(v.getNum1()) < 0) {
                                v.setJiegpd("符合");
                            } else {
                                v.setJiegpd("不符合");
                            }
                        } else {
                            v.setJiegpd("-");
                        }
                    } else {
                        throw new ServiceException(queryById(v.getQualId()).getQualName() + "技术指标不完整，无法计算");
                    }
                    break;
                case "02":
                    if (ObjUtil.isNotNull(v.getNum1())) {
                        v.setJiszb(dictService.selectDictLabel("symbol",
                            "02") + BigDecimalUtil.toPlain(v.getNum1()));
                        if (ObjUtil.isNotNull(v.getJiancjg())) {
                            if (v.getJiancjg().compareTo(v.getNum1()) > 0) {
                                v.setJiegpd("符合");
                            } else {
                                v.setJiegpd("不符合");
                            }
                        } else {
                            v.setJiegpd("-");
                        }
                    } else {
                        throw new ServiceException(queryById(v.getQualId()).getQualName() + "技术指标不完整，无法计算");
                    }
                    break;
                case "03":
                    if (ObjUtil.isNotNull(v.getNum1())) {
                        v.setJiszb(dictService.selectDictLabel("symbol",
                            "03") + BigDecimalUtil.toPlain(v.getNum1()));
                        if (ObjUtil.isNotNull(v.getJiancjg())) {
                            if (v.getJiancjg().compareTo(v.getNum1()) == 0) {
                                v.setJiegpd("符合");
                            } else {
                                v.setJiegpd("不符合");
                            }
                        } else {
                            v.setJiegpd("-");
                        }
                    } else {
                        throw new ServiceException(queryById(v.getQualId()).getQualName() + "技术指标不完整，无法计算");
                    }
                    break;
                case "04":
                    if (ObjUtil.isNotNull(v.getNum1())) {
                        v.setJiszb(dictService.selectDictLabel("symbol",
                            "04") + BigDecimalUtil.toPlain(v.getNum1()));
                        if (ObjUtil.isNotNull(v.getJiancjg())) {
                            if (v.getJiancjg().abs().compareTo(v.getNum1().abs()) <= 0) {
                                v.setJiegpd("符合");
                            } else {
                                v.setJiegpd("不符合");
                            }
                        } else {
                            v.setJiegpd("-");
                        }
                    } else {
                        throw new ServiceException(queryById(v.getQualId()).getQualName() + "技术指标不完整，无法计算");
                    }
                    break;
                case "05":
                    if (ObjUtil.isNotNull(v.getNum1())) {
                        v.setJiszb(dictService.selectDictLabel("symbol",
                            "05") + BigDecimalUtil.toPlain(v.getNum1()));
                        if (ObjUtil.isNotNull(v.getJiancjg())) {
                            if (v.getJiancjg().abs().compareTo(v.getNum1().abs()) >= 0) {
                                v.setJiegpd("符合");
                            } else {
                                v.setJiegpd("不符合");
                            }
                        } else {
                            v.setJiegpd("-");
                        }
                    } else {
                        throw new ServiceException(queryById(v.getQualId()).getQualName() + "技术指标不完整，无法计算");
                    }
                    break;
                case "06":
                    if (ObjUtil.isNotNull(v.getNum1())) {
                        v.setJiszb(dictService.selectDictLabel("symbol",
                            "06") + BigDecimalUtil.toPlain(v.getNum1()));
                        if (ObjUtil.isNotNull(v.getJiancjg())) {
                            if (v.getJiancjg().abs().compareTo(v.getNum1().abs()) <= 0) {
                                v.setJiegpd("符合");
                            } else {
                                v.setJiegpd("不符合");
                            }
                        } else {
                            v.setJiegpd("-");
                        }
                    } else {
                        throw new ServiceException(queryById(v.getQualId()).getQualName() + "技术指标不完整，无法计算");
                    }
                    break;
                case "07":
                    if (ObjUtil.isNotNull(v.getNum1()) && ObjUtil.isNotNull(v.getNum2())) {
                        v.setJiszb("≥" + BigDecimalUtil.toPlain(v.getNum1()) + ",≤" + BigDecimalUtil.toPlain(v.getNum2()));
                        if (ObjUtil.isNotNull(v.getJiancjg())) {
                            if (v.getJiancjg().compareTo(v.getNum1()) >= 0 && v.getJiancjg().compareTo(v.getNum2()) <= 0) {
                                v.setJiegpd("符合");
                            } else {
                                v.setJiegpd("不符合");
                            }
                        } else {
                            v.setJiegpd("-");
                        }
                    } else {
                        throw new ServiceException(queryById(v.getQualId()).getQualName() + "技术指标不完整，无法计算");
                    }
                    break;
                case "08":
                    if (ObjUtil.isNotNull(v.getNum1()) && ObjUtil.isNotNull(v.getNum2())) {
                        v.setJiszb(">" + BigDecimalUtil.toPlain(v.getNum1()) + ",≤" + BigDecimalUtil.toPlain(v.getNum2()));
                        if (ObjUtil.isNotNull(v.getJiancjg())) {
                            if (v.getJiancjg().compareTo(v.getNum1()) > 0 && v.getJiancjg().compareTo(v.getNum2()) <= 0) {
                                v.setJiegpd("符合");
                            } else {
                                v.setJiegpd("不符合");
                            }
                        } else {
                            v.setJiegpd("-");
                        }
                    } else {
                        throw new ServiceException(queryById(v.getQualId()).getQualName() + "技术指标不完整，无法计算");
                    }
                    break;
                case "09":
                    if (ObjUtil.isNotNull(v.getNum1()) && ObjUtil.isNotNull(v.getNum2())) {
                        v.setJiszb("≥" + BigDecimalUtil.toPlain(v.getNum1()) + ",<" + BigDecimalUtil.toPlain(v.getNum2()));
                        if (ObjUtil.isNotNull(v.getJiancjg())) {
                            if (v.getJiancjg().compareTo(v.getNum1()) >= 0 && v.getJiancjg().compareTo(v.getNum2()) < 0) {
                                v.setJiegpd("符合");
                            } else {
                                v.setJiegpd("不符合");
                            }
                        } else {
                            v.setJiegpd("-");
                        }
                    } else {
                        throw new ServiceException(queryById(v.getQualId()).getQualName() + "技术指标不完整，无法计算");
                    }
                    break;
                case "10":
                    if (ObjUtil.isNotNull(v.getNum1()) && ObjUtil.isNotNull(v.getNum2())) {
                        v.setJiszb(">" + BigDecimalUtil.toPlain(v.getNum1()) + ",<" + BigDecimalUtil.toPlain(v.getNum2()));
                        if (ObjUtil.isNotNull(v.getJiancjg())) {
                            if (v.getJiancjg().compareTo(v.getNum1()) > 0 && v.getJiancjg().compareTo(v.getNum2()) < 0) {
                                v.setJiegpd("符合");
                            } else {
                                v.setJiegpd("不符合");
                            }
                        } else {
                            v.setJiegpd("-");
                        }
                    } else {
                        throw new ServiceException(queryById(v.getQualId()).getQualName() + "技术指标不完整，无法计算");
                    }
                    break;
            }
        });
        return voList;
    }

}
